# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

load("//bazel:pl_build_system.bzl", "pl_cc_library", "pl_cc_test")

package(default_visibility = ["//src/stirling:__subpackages__"])

pl_cc_library(
    name = "cc_library",
    srcs = glob(
        ["*.cc"],
        exclude = [
            "**/*_test.cc",
        ],
    ),
    hdrs = glob(["*.h"]),
    deps = [
        "//src/common/exec:cc_library",
        "//src/common/grpcutils:cc_library",
        "//src/stirling/bpf_tools:cc_library",
        "//src/stirling/core:cc_library",
        "//src/stirling/obj_tools:cc_library",
        "//src/stirling/source_connectors/socket_tracer/bcc_bpf:socket_trace",
        "//src/stirling/source_connectors/socket_tracer/bcc_bpf_intf:cc_library",
        "//src/stirling/source_connectors/socket_tracer/proto:sock_event_pl_cc_proto",
        "//src/stirling/source_connectors/socket_tracer/protocols:cc_library",
        "//src/stirling/utils:cc_library",
    ],
)

pl_cc_test(
    name = "socket_trace_connector_test",
    srcs = ["socket_trace_connector_test.cc"],
    deps = [
        ":cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_test(
    name = "uprobe_symaddrs_test",
    srcs = ["uprobe_symaddrs_test.cc"],
    data = [
        "//src/stirling/testing/demo_apps/go_grpc_tls_pl/server",
        "//src/stirling/testing/demo_apps/node:node_debug",
    ],
    deps = [
        ":cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/utils:cc_library",
    ],
)

pl_cc_test(
    name = "data_stream_test",
    srcs = ["data_stream_test.cc"],
    deps = [
        ":cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
    ],
)

pl_cc_test(
    name = "conn_tracker_test",
    srcs = ["conn_tracker_test.cc"],
    deps = [
        ":cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
    ],
)

pl_cc_test(
    name = "conn_trackers_manager_test",
    srcs = ["conn_trackers_manager_test.cc"],
    deps = [
        ":cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
    ],
)

pl_cc_test(
    name = "conn_tracker_http2_test",
    srcs = ["conn_tracker_http2_test.cc"],
    deps = [
        ":cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
    ],
)

pl_cc_test(
    name = "conn_stats_test",
    srcs = ["conn_stats_test.cc"],
    deps = [
        ":cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
    ],
)

pl_cc_test(
    name = "fd_resolver_test",
    srcs = ["fd_resolver_test.cc"],
    deps = [
        ":cc_library",
        "//src/common/system:cc_library",
    ],
)

###############################################################################
# BPF Tests
###############################################################################

pl_cc_test(
    name = "socket_trace_bpf_test",
    timeout = "long",
    srcs = ["socket_trace_bpf_test.cc"],
    flaky = True,
    shard_count = 5,
    tags = ["requires_bpf"],
    deps = [
        ":cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_test(
    name = "conn_stats_bpf_test",
    timeout = "moderate",
    srcs = ["conn_stats_bpf_test.cc"],
    flaky = True,
    shard_count = 2,
    tags = ["requires_bpf"],
    deps = [
        ":cc_library",
        "//src/common/exec:cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_test(
    name = "http_trace_bpf_test",
    timeout = "moderate",
    srcs = ["http_trace_bpf_test.cc"],
    flaky = True,
    shard_count = 3,
    tags = [
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/exec:cc_library",
        "//src/stirling/source_connectors/socket_tracer/protocols/http/testing:go_http_fixture",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_test(
    name = "mux_trace_bpf_test",
    timeout = "moderate",
    srcs = ["mux_trace_bpf_test.cc"],
    flaky = True,
    tags = [
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/protocols/test_output_generator:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_test(
    name = "mysql_trace_bpf_test",
    timeout = "moderate",
    srcs = ["mysql_trace_bpf_test.cc"],
    data = [
        "//src/stirling/source_connectors/socket_tracer/protocols/mysql/testing:expected_outputs",
        "//src/stirling/source_connectors/socket_tracer/protocols/mysql/testing:scripts",
    ],
    flaky = True,
    tags = [
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/protocols/test_output_generator:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_test(
    name = "cql_trace_bpf_test",
    timeout = "moderate",
    srcs = [
        "cql_trace_bpf_test.cc",
    ],
    flaky = True,
    tags = [
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_test(
    name = "pgsql_trace_bpf_test",
    timeout = "moderate",
    srcs = [
        "pgsql_trace_bpf_test.cc",
    ],
    flaky = True,
    shard_count = 2,
    tags = [
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_test(
    name = "grpc_trace_bpf_test",
    timeout = "moderate",
    srcs = ["grpc_trace_bpf_test.cc"],
    data = [
        "//src/stirling/source_connectors/socket_tracer/protocols/http2/testing/go_grpc_client",
        "//src/stirling/source_connectors/socket_tracer/protocols/http2/testing/go_grpc_server",
    ],
    # TODO(PL-1412): GRPCTraceUprobingTest.CaptureRPCTraceRecord sometimes captures no data.
    flaky = True,
    shard_count = 3,
    tags = [
        "requires_bpf",
        # This test runs Go gRPC client & server, which causes other tests to attach & detach
        # uprobes and slowed down significantly.
        "exclusive",
        "no_asan",
    ],
    deps = [
        ":cc_library",
        "//src/common/exec:cc_library",
        "//src/stirling/source_connectors/socket_tracer/protocols/http2/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/protocols/http2/testing/proto:greet_pl_cc_proto",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_test(
    name = "http2_trace_bpf_test",
    timeout = "moderate",
    srcs = ["http2_trace_bpf_test.cc"],
    flaky = True,
    tags = [
        "requires_bpf",
        # This test runs Go gRPC client & server, which causes other tests to attach & detach
        # uprobes, slowing them down significantly.
        "exclusive",
        "no_asan",
    ],
    deps = [
        ":cc_library",
        "//src/common/exec:cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_test(
    name = "dns_trace_bpf_test",
    timeout = "moderate",
    srcs = [
        "dns_trace_bpf_test.cc",
    ],
    flaky = True,
    tags = [
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_test(
    name = "redis_trace_bpf_test",
    timeout = "long",
    srcs = ["redis_trace_bpf_test.cc"],
    flaky = True,
    tags = ["requires_bpf"],
    deps = [
        ":cc_library",
        "//src/common/exec:cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_test(
    name = "nats_trace_bpf_test",
    timeout = "long",
    srcs = ["nats_trace_bpf_test.cc"],
    flaky = True,
    tags = ["requires_bpf"],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
    ],
)

pl_cc_test(
    name = "kafka_trace_bpf_test",
    timeout = "moderate",
    srcs = ["kafka_trace_bpf_test.cc"],
    flaky = True,
    tags = ["requires_bpf"],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
    ],
)

pl_cc_test(
    name = "openssl_trace_bpf_test",
    timeout = "long",
    srcs = ["openssl_trace_bpf_test.cc"],
    flaky = True,
    shard_count = 2,
    tags = [
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_test(
    name = "dyn_lib_trace_bpf_test",
    timeout = "moderate",
    srcs = ["dyn_lib_trace_bpf_test.cc"],
    flaky = True,
    tags = [
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_test(
    name = "go_tls_trace_bpf_test",
    timeout = "moderate",
    srcs = ["go_tls_trace_bpf_test.cc"],
    flaky = True,
    tags = [
        "exclusive",
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/exec:cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/testing:cc_library",
    ],
)
